home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Offline Browsing / HTTrack.exe / data1.cab / Sources / src / htsjava.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-28  |  9.4 KB  |  415 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Java classes parser                                    */
  34. /* Author: Yann Philippot                                       */
  35. /* ------------------------------------------------------------ */
  36.  
  37.  
  38. /* Version: Oct/2000 */
  39. /* Fixed: problems with class structure (10/2000) */
  40.  
  41. // htsjava.c - Parseur de classes java
  42.  
  43. #include "stdio.h"
  44. #include "htssystem.h"
  45. #include "httrack.h"
  46. #include "htsjava.h"
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. //#include <math.h>
  52.  
  53. #ifndef HTS_LITTLE_ENDIAN
  54. #define REVERSE_ENDIAN 1
  55. #else
  56. #define REVERSE_ENDIAN 0
  57. #endif
  58.  
  59. /* big/little endian swap */
  60. #define hts_swap16(A) ( (((A) & 0xFF)<<8) | (((A) & 0xFF00)>>8) )
  61. #define hts_swap32(A) ( (( (hts_swap16(A)) & 0xFFFF)<<16) | (( (hts_swap16(A>>16)) & 0xFFFF)) )
  62.  
  63.  
  64. // ** HTS_xx sinon pas pris par VC++
  65. #define HTS_CLASS  7
  66. #define HTS_FIELDREF  9
  67. #define HTS_METHODREF  10
  68. #define HTS_STRING  8
  69. #define HTS_INTEGER  3
  70. #define HTS_FLOAT  4
  71. #define HTS_LONG  5
  72. #define HTS_DOUBLE  6
  73. #define HTS_INTERFACE  11
  74. #define HTS_NAMEANDTYPE  12
  75. #define HTS_ASCIZ  1
  76. #define HTS_UNICODE  2
  77.  
  78. #define JAVADEBUG 0
  79.  
  80. int hts_parse_java(char *file,char* err_msg)
  81. {
  82.   FILE *fpout;
  83.   JAVA_HEADER header;
  84.   RESP_STRUCT *tab;
  85.   
  86. #if JAVADEBUG
  87.   printf("fopen\n");
  88. #endif
  89.   if ((fpout = fopen(fconv(file), "r+b")) == NULL)
  90.   {
  91.     //fprintf(stderr, "Cannot open input file.\n");
  92.     sprintf(err_msg,"Unable to open file %s",file);
  93.     return 0;   // une erreur..
  94.   }
  95.     
  96. #if JAVADEBUG
  97.   printf("fread\n");
  98. #endif
  99.   //if (fread(&header,1,sizeof(JAVA_HEADER),fpout) != sizeof(JAVA_HEADER)) {   // pas complet..
  100.   if (fread(&header,1,10,fpout) != 10) {   // pas complet..
  101.     fclose(fpout);
  102.     sprintf(err_msg,"File header too small (file len = "LLintP")",(LLint)fsize(file));
  103.     return 0;
  104.   }
  105.  
  106. #if JAVADEBUG
  107.   printf("header\n");
  108. #endif
  109.   // tester en tΩte
  110. #if REVERSE_ENDIAN
  111.   header.magic = hts_swap32(header.magic);
  112.   header.count = hts_swap16(header.count); 
  113. #endif        
  114.   if(header.magic!=0xCAFEBABE) {
  115.     sprintf(err_msg,"non java file");
  116.     return 0;
  117.   }
  118.  
  119.   tab =(RESP_STRUCT*)calloct(header.count,sizeof(RESP_STRUCT));
  120.   if (!tab) {
  121.     sprintf(err_msg,"Unable to alloc %d bytes",sizeof(RESP_STRUCT));
  122.     fclose(fpout);
  123.     return 0;    // erreur..
  124.   }
  125.  
  126. #if JAVADEBUG
  127.   printf("calchead\n");
  128. #endif
  129.   {
  130.     int i;
  131.     
  132.     for (i = 1; i < header.count; i++) {
  133.       int err=0;  // ++    
  134.       tab[i]=readtable(fpout,tab[i],&err,err_msg);
  135.       if (!err) {
  136.         if ((tab[i].type == HTS_LONG) ||(tab[i].type == HTS_DOUBLE)) i++;  //2 element si double ou float
  137.       } else {    // ++ une erreur est survenue!
  138.         if (strnotempty(err_msg)==0)
  139.           strcpy(err_msg,"Internal readtable error");
  140.         freet(tab);
  141.         fclose(fpout);
  142.         return 0;
  143.       }
  144.     }
  145.     
  146.   }
  147.  
  148.   
  149. #if JAVADEBUG
  150.   printf("addfiles\n");
  151. #endif
  152.   {
  153.     unsigned int acess;
  154.     unsigned int Class;
  155.     unsigned int SClass;
  156.     int i;
  157.     acess = readshort(fpout);
  158.     Class = readshort(fpout);
  159.     SClass = readshort(fpout);
  160.     
  161.     for (i = 1; i <header.count; i++) {
  162.       
  163.       if (tab[i].type == HTS_CLASS) {
  164.         
  165.         if ((tab[i].index1<header.count) && (tab[i].index1>=0)) {
  166.           
  167.           
  168.           if((tab[i].index1!=SClass) && (tab[i].index1!=Class) && (tab[tab[i].index1].name[0]!='[')) {
  169.             
  170.             if(!strstr(tab[tab[i].index1].name,"java/")) {
  171.               char tempo[1024];
  172.               tempo[0]='\0';
  173.               
  174.               sprintf(tempo,"%s.class",tab[tab[i].index1].name);
  175. #if JAVADEBUG
  176.               printf("add %s\n",tempo);
  177. #endif
  178.               if (tab[tab[i].index1].file_position >= 0)
  179.                 hts_add_file(tempo,tab[tab[i].index1].file_position);
  180.             }
  181.             
  182.           }
  183.         } else { 
  184.           i=header.count;  // exit 
  185.         }
  186.       }
  187.       
  188.     }
  189.   }
  190.   
  191.  
  192. #if JAVADEBUG
  193.   printf("end\n");
  194. #endif
  195.   freet(tab);
  196.   fclose(fpout);
  197.   return 1;
  198. }
  199.  
  200.  
  201.  
  202.  
  203. // error: !=0 si erreur fatale
  204. RESP_STRUCT readtable(FILE *fp,RESP_STRUCT trans,int* error,char* err_msg)
  205. {
  206.   unsigned short int length;
  207.   int j;
  208.   *error = 0;  // pas d'erreur
  209.   trans.file_position=-1;
  210.   trans.type = (int)(unsigned char)fgetc(fp);
  211.   switch (trans.type) {
  212.   case HTS_CLASS:
  213.     strcpy(trans.name,"Class");
  214.     trans.index1 = readshort(fp);
  215.     break;
  216.     
  217.   case HTS_FIELDREF:
  218.     strcpy(trans.name,"Field Reference");
  219.     trans.index1 = readshort(fp);
  220.     readshort(fp);
  221.     break;
  222.     
  223.   case HTS_METHODREF:
  224.     strcpy(trans.name,"Method Reference");
  225.     trans.index1 = readshort(fp);
  226.     readshort(fp);
  227.     break;
  228.     
  229.   case HTS_INTERFACE:
  230.     strcpy(trans.name,"Interface Method Reference");
  231.     trans.index1 =readshort(fp);
  232.     readshort(fp);
  233.     break;
  234.   case HTS_NAMEANDTYPE:
  235.     strcpy(trans.name,"Name and Type");
  236.     trans.index1 = readshort(fp);
  237.     readshort(fp);
  238.     break;
  239.     
  240.   case HTS_STRING:                // CONSTANT_String
  241.     strcpy(trans.name,"String");
  242.     trans.index1 = readshort(fp);
  243.     break;
  244.     
  245.   case HTS_INTEGER:
  246.     strcpy(trans.name,"Integer");
  247.     for(j=0;j<4;j++) fgetc(fp);
  248.     break;
  249.     
  250.   case HTS_FLOAT:
  251.     strcpy(trans.name,"Float");
  252.     for(j=0;j<4;j++) fgetc(fp);
  253.     break;
  254.     
  255.   case HTS_LONG:
  256.     strcpy(trans.name,"Long");
  257.     for(j=0;j<8;j++) fgetc(fp);
  258.     break;
  259.   case HTS_DOUBLE:
  260.     strcpy(trans.name,"Double");
  261.     for(j=0;j<8;j++) fgetc(fp);
  262.     break;
  263.     
  264.   case HTS_ASCIZ:
  265.   case HTS_UNICODE:
  266.     
  267.     if (trans.type == HTS_ASCIZ)
  268.       strcpy(trans.name,"HTS_ASCIZ");
  269.     else
  270.       strcpy(trans.name,"HTS_UNICODE");
  271.     
  272.     {
  273.       char buffer[1024]; 
  274.       char *p;
  275.       
  276.       p=&buffer[0];
  277.  
  278.       //fflush(fp); 
  279.       trans.file_position=ftell(fp);
  280.       length = readshort(fp);
  281.       if (length<HTS_URLMAXSIZE) {
  282.         // while ((length > 0) && (length<500)) {
  283.         while (length > 0) {
  284.           *p++ =fgetc(fp);
  285.           
  286.           length--;
  287.         }
  288.         *p='\0';
  289.         
  290.         //#if JDEBUG
  291.         //      if(tris(buffer)==1) printf("%s\n ",buffer);
  292.         //      if(tris(buffer)==2)  printf("%s\n ",printname(buffer));
  293.         //#endif
  294.         if(tris(buffer)==1)  hts_add_file(buffer,trans.file_position);       
  295.         else if(tris(buffer)==2)  hts_add_file(printname(buffer),trans.file_position);
  296.  
  297.         strcpy(trans.name,buffer);
  298.       } else {    // gros pb
  299.         while ( (length > 0) && (!feof(fp))) {
  300.           fgetc(fp);
  301.           length--;
  302.         }
  303.         if (!feof(fp)) {
  304.           trans.type=-1;
  305.         } else {
  306.           sprintf(err_msg,"Internal stucture error (ASCII)");
  307.           *error = 1;
  308.         }
  309.         return(trans);
  310.       }
  311.     }
  312.     break;
  313.   default:
  314.     // printf("Type inconnue\n");
  315.     // on arrΩte tout 
  316.     sprintf(err_msg,"Internal structure unknown (type %d)",trans.type);
  317.     *error = 1;
  318.     return(trans);
  319.     break;
  320.   }  
  321.   return(trans);
  322. }
  323.  
  324.  
  325. unsigned short int readshort(FILE *fp)
  326. {
  327.   unsigned short int valint;
  328.   fread(&valint,sizeof(valint),1,fp);
  329.  
  330. #if REVERSE_ENDIAN
  331.   return hts_swap16(valint);
  332. #else
  333.   return valint;
  334. #endif
  335.   
  336. }
  337.  
  338. /*
  339.  
  340.   Darn, this one was really dirty
  341.  
  342. // a^b
  343. double my_pow(double a,double b) {
  344.   double r=1;
  345.   int i;
  346.   for(i=0;i<b;i++)
  347.     r*=a;
  348.   return r;
  349. }
  350.  
  351. unsigned int swap(long int nomber,int digit)
  352. {
  353.   long unsigned  int t,i;
  354.   long unsigned int d=0xff;
  355.   double total=0;
  356.   for(i=0;i<(long unsigned  int) digit;i++){ // **
  357.     t=nomber & d;
  358.     total=total+t*my_pow((double)16,(double)(digit*2-i*2-2));
  359.     nomber=nomber >> 8;
  360.   }
  361.   return (int)total;
  362. }
  363. */
  364.  
  365. int tris(char * buffer)
  366. {
  367.   //
  368.   // Java
  369.   if((buffer[0]=='[') && buffer[1]=='L' && (!strstr(buffer,"java/")) ) 
  370.     return 2;
  371.   if (strstr(buffer,".gif") || strstr(buffer,".jpg") || strstr(buffer,".jpeg") || strstr(buffer,".au") ) 
  372.     return 1;
  373.   // Ajouts R.X: test type
  374.   // Autres fichiers
  375.   {
  376.     char type[256];
  377.     type[0]='\0';
  378.     get_httptype(type,buffer,0);
  379.     if (strnotempty(type))     // type reconnu!
  380.       return 1;
  381.   }
  382.   return 0;
  383. }
  384.  
  385.  
  386. char * printname(char  name[1024])
  387. {
  388.   static char rname[1024];
  389.   //char *rname;
  390.   char *p;
  391.   char *p1;
  392.   int j;
  393.   rname[0]='\0';
  394.   //
  395.  
  396.   p=&name[0];
  397.   
  398.   if(*p!='[')  return "";
  399.   p+=2;
  400.   //rname=(char*)calloct(strlen(name)+8,sizeof(char));
  401.   p1=rname;
  402.   for (j = 0; j < (int) strlen(name); j++,p++) {
  403.     if (*p == '/') *p1='.'; 
  404.     if (*p==';'){*p1='\0';
  405.     strcat(rname,".class");
  406.     return (rname);}
  407.     else *p1=*p;
  408.     p1++;
  409.   }
  410.   p1-=3;
  411.   *p1='\0';
  412.   return (rname);
  413.   
  414. }
  415.